package com.york.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.york.eduservice.entity.EduTeacher;
import com.york.eduservice.entity.vo.TeacherQuery;
import com.york.eduservice.service.EduTeacherService;
import com.york.utils.exception.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:讲师 前端控制器
 * @Data: 2022/3/7 15:39
 * @Author: yangboxiong
 * @Version
 * @since JDK 1.8
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询讲师表的所有数据
     *
     * @return
     */
    @ApiOperation(value = "查询讲师表的所有数据")
    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list).message("获取成功");
    }

    /**
     * 逻辑删除讲师功能
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除讲师id")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id) {
        boolean flag = eduTeacherService.removeById(id);
        return flag ? R.ok().message("删除成功") : R.error().message("删除失败");
    }

    /**
     * 分页查询讲师
     *
     * @param current 当前页
     * @param limit   每页记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageListTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法实现，底层封装，把分页所有数据封装到pageTeacher对象中
        eduTeacherService.page(pageTeacher, null);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 多条件查询带分页的方法
     *
     * @param current      当前页
     * @param limit        每页记录数
     * @param teacherQuery 教师查询
     * @return
     */
    @ApiOperation(value = "多条件查询带分页")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") long current, @PathVariable("limit") long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件,姓名
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            //构建条件，头衔
            wrapper.like("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //构建条件，查询开始时间
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //构建条件，查询结束时间
            wrapper.le("gmt_modified", end);
        }
        //根据降序排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        eduTeacherService.page(pageTeacher, wrapper);
        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师接口的方法
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "添加讲师接口")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        return save ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    /**
     * 根据讲师id进行查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable("id") String id) {
        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher", byId);
    }

    /**
     * 修改讲师接口方法
     *
     * @param eduTeacher
     * @return
     */
    @ApiOperation(value = "修改讲师接口")
    @PutMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean update = eduTeacherService.updateById(eduTeacher);
        return update ? R.ok().message("修改成功") : R.error().message("修改失败");
    }
}

